﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TKB.Services.BusinessObjects;

public partial class Ithalat_MuayeneListesi : TKBBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BilgileriYukle();
            JavascriptHelper.PopUpWindowEventiEkle(HyperLinkEkle, "~/Ithalat/PopUp/MuayeneEkle.aspx", 1000, 650, true);
        }
        ((MasterPage_Main)this.Master).MenuToLoad = SiteMapMenus.TKB;
    }

    private void BilgileriYukle()
    {
        DataTable dt = new MuayeneBS().SelectAllMuayene();
        BindEt(dt);
    }

    private void BindEt(DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            SiraNoEkle(dt);
            gvMuayeneler.DataSource = dt;
            gvMuayeneler.DataBind();
        }
        else
        {
            gvMuayeneler.DataSource = null;
            gvMuayeneler.DataBind();
        }
    }

    private DataTable SiraNoEkle(DataTable dt)
    {
        dt.Columns.Add("SiraNo", System.Type.GetType("System.Int32"));
        int siraNo = 1;
        foreach (DataRow row in dt.Rows)
        {
            row["SiraNo"] = siraNo;
            siraNo++;
        }
        return dt;
    }

    protected void ButtonMuayeneAra_Click(object sender, EventArgs e)
    {

    }

    protected void ButtonTemizle_Click(object sender, EventArgs e)
    {

    }

    protected void gvMuayeneler_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow)
            return;

        string muayeneKey = gvMuayeneler.DataKeys[e.Row.RowIndex].Values["MuayeneKey"].ToString();
        string queryString = "?MuayeneKey=" + muayeneKey;
        string navigateUrlEkle = "~/Ithalat/PopUp/MuayeneEkle.aspx" + queryString;
        string navigateUrlRapor = "~/Ithalat/PopUp/MuayeneYazdir.aspx" + queryString;
        string islemModu = "&IslemModu=";

        HyperLink hyperLinkDetay = (HyperLink)e.Row.FindControl("HyperLinkDetayGoruntule");
        if (hyperLinkDetay != null)
        {
            JavascriptHelper.PopUpWindowEventiEkle(hyperLinkDetay, navigateUrlEkle + islemModu + "Goruntule", 1000, 650);
        }
        HyperLink hyperLinkGuncelle = (HyperLink)e.Row.FindControl("HyperLinkGuncelle");
        if (hyperLinkGuncelle != null)
        {
            JavascriptHelper.PopUpWindowEventiEkle(hyperLinkGuncelle, navigateUrlEkle + islemModu + "Guncelle", 1000, 650);
        }
        HyperLink hyperLinRapor = (HyperLink)e.Row.FindControl("HyperLinkRapor");
        if (hyperLinRapor != null)
        {
            JavascriptHelper.PopUpWindowEventiEkle(hyperLinRapor, navigateUrlRapor, 750, 850);
        }
    }

    protected void gvMuayeneler_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvMuayeneler.PageIndex = e.NewPageIndex;
        DataTable dt = (DataTable)ViewState["Liste"];
        gvMuayeneler.DataSource = dt;
        gvMuayeneler.DataBind();
    }

    protected void gvMuayeneler_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dt = ((DataTable)ViewState["Liste"]);
        ViewState["Sorting"] = ViewState["Sorting"] == null ? " ASC" : ViewState["Sorting"];

        if (ViewState["Sorting"].ToString() == " DESC")
        {
            dt.DefaultView.Sort = e.SortExpression + ViewState["Sorting"].ToString();
            ViewState["Sorting"] = " ASC";
        }
        else
        {
            dt.DefaultView.Sort = e.SortExpression + ViewState["Sorting"].ToString();
            ViewState["Sorting"] = " DESC";
        }
        gvMuayeneler.DataSource = dt;
        gvMuayeneler.DataBind();

        ViewState["Liste"] = dt;
    }

}
